// pages/zlk/zlk.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    session_key:'',
    tabCur: 0, //默认选中
    tabs: [{
        name: '2022考研大纲解读',
        id: 0
      },
      {
        name: '2021考研大纲解读',
        id: 1
      },

    ],
    list: 0,
    lists: [
      [{
        title: '英语大纲解读',
        sid: 1
      },{
        title: '政治大纲解读',
        sid: 2
      }],
    [{
        title: '英语大纲解读',
        sid: 3
      },{
        title: '政治大纲解读',
        sid: 4
      },{
        title: '数学大纲解读',
        sid: 3
      }
    ]],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    console.log(options);
    that.setData({
      area: options.area,
      geneal: options.agent
    })
    if (options.page) {
      that.setData({
        page: options.page
      })
    }
  },
  topdf(e) {
    var that = this;
    var phone = wx.getStorageSync('phone')
    if(phone){
      console.log(e.currentTarget.dataset.sid)
      var sid = e.currentTarget.dataset.sid;
      wx.navigateTo({
        url: '../kypdf/kypdf?sid=' + sid,
      })
    } else {
      wx.showToast({
        title: '请先登录',
        icon: 'none',
        duration: 2000
      });
      that.setData({
        login:1
      })
    }
  },

  getPhoneNumber(e) {
    var that = this;
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)

    wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
      that.setData({
        mobile: res.result,
      })
      // wx.setStorageSync('mqjphone', res.result)
      console.log(res.result)
      wx.request({ //发送请求
        url: 'https://zg99.offcn.com/index/biaodan/register?actid=33633&callback=?', //接收url
        data: {
          phone: res.result,
          fenxiao: that.data.area,
          geneal: that.data.geneal
        }, //接收参数
        success(res) {
          wx.setStorageSync('phone', that.data.mobile) 
          console.log(res)
            var jsonStr = res.data;
            jsonStr = jsonStr.replace("(", "");
            jsonStr = jsonStr.replace(")", "");
            if (typeof jsonStr != 'object') {
                jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                var theJson = JSON.parse(jsonStr);
            }
            console.log(theJson)
            that.setData({
                login: 0
            })
        }
    });


    wx.request({
      url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
      method: 'POST',
      data: {
        activeId: 25,
        mobile: res.result,
        areaCode: that.data.area,
        agent: that.data.geneal,
        pageId: that.data.page
      },
      success: res => {
        console.log(res);
      }
    })
  }).catch(err => {
    console.error(err);
  });

    wx.login({
      success(res) {
        console.log(res)
        if (res.code) {
          wx.request({
            url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/program/wx_login',
            method: 'POST',
            data: {
              code: res.code,
              id: that.data.activity_id,
              phone: that.data.mobile,
              fenxiao: that.data.area,
              geneal: that.data.geneal
            },
            success: res => {
            
              that.setData({
                session_key: res.data.session_key
              })
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
    // wx.request({
    //   url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/program/get_phone_num',
    //   method: 'POST',
    //   data: {
    //     id: that.data.activity_id,
    //     phone: that.data.mobile,
    //     fenxiao: that.data.area,
    //     geneal: that.data.geneal,
    //     iv: e.detail.iv,
    //     encryptedData: e.detail.encryptedData,
    //   },
    //   success: res => {
    //     wx.setStorageSync('sqzlphone', that.data.mobile)    
    //   }
    // })


  },

  //选择条目
  tabSelect(e) {
    console.log(e.currentTarget.dataset.id)
    this.setData({
      list: e.currentTarget.dataset.id,
      tabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 2) * 200
    })
  },
  tohb(){
    wx.navigateTo({
      url: '../tuiwen/tuiwen',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  // onLoad: function (options) {
  //   var that = this;
  //   console.log(options);
  //   that.setData({
  //     area: options.area,
  //     geneal: options.agent
  //   })
  //   if (options.page) {
  //     that.setData({
  //       page: options.page
  //     })
  //   }

  //   wx.login({
  //     success (res) {
  //       console.log(res)
  //       if (res.code) {
  //         wx.request({
  //           url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/program/wx_login',
  //           method:'POST',
  //           data: {
  //             code: res.code,
  //             id: that.data.activity_id
  //           },
  //           success: res => {
  //             console.log(res.data.session_key);
  //             that.setData({
  //               session_key : res.data.session_key
  //             })
  //           }
  //         })
  //       } else {
  //         console.log('登录失败！' + res.errMsg)
  //       }
  //     }
  //   })
  // },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})